home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / btree.h < prev    next >
C/C++ Source or Header  |  1999-03-14  |  6KB  |  184 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- //
  5. // C++ Header File Name: btree.h 
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer   
  8. // File Creation Date: 02/12/1997 
  9. // Date Last Modified: 03/15/1999
  10. // Copyright (c) 1997 Douglas M. Gaer
  11. // ----------------------------------------------------------- // 
  12. // ---------- Include File Description and Details  ---------- // 
  13. // ----------------------------------------------------------- // 
  14. /*
  15. The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
  16. All those who put this code or its derivatives in a commercial
  17. product MUST mention this copyright in their documentation for
  18. users of the products in which this code or its derivative
  19. classes are used. Otherwise, you have the freedom to redistribute
  20. verbatim copies of this source code, adapt it to your specific
  21. needs, or improve the code and release your improvements to the
  22. public provided that the modified files carry prominent notices
  23. stating that you changed the files and the date of any change.
  24.  
  25. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  26. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  27. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  28. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  29. CORRECTION.
  30.  
  31. Balanced multi-way file-base tree class used to create
  32. file-based objects that reside on disk.
  33.  
  34. Changes:
  35. ================================================================
  36. 10/08/1998: Added the ReInit() function to allow an application
  37. to flush the cache and reinitialize the Btree. This will ensure
  38. that the memory buffers and the file data stays in sync during
  39. multiple file access and multiple instances of the application. 
  40. Added by: Doug Gaer
  41.  
  42. 10/08/1998: Added the TestTree() functions to ensure that the
  43. memory buffers and the file data stay in sync during multiple
  44. file access.
  45. Added by: Doug Gaer
  46.  
  47. 10/08/1998: Modified all versions of the Search() function to
  48. test the tree before searching.
  49. Changed by: Doug Gaer
  50.  
  51. 10/08/1998: Modified all versions of the FullSearch() function 
  52. to test the tree before searching.
  53. Changed by: Doug Gaer
  54.  
  55. 10/08/1998: Modified the Add() function to flush the disk
  56. buffers and the cache after each insertion by default.
  57. Changed by: Doug Gaer
  58.  
  59. 10/08/1998: Modifed the Remove() function to flush the disk
  60. buffers and the cache after each deletion by default.
  61. Changed by: Doug Gaer
  62. */
  63. // ----------------------------------------------------------- //   
  64. #ifndef __BTREE_HPP
  65. #define __BTREE_HPP
  66.  
  67. // NOTE: To avoid portability problems with template classes, enable
  68. // the __NOT_USING_TEMPLATE_CLASS__ macro and directly code the
  69. // Bucket class, Cache class, and the CachePtr class for the data
  70. // type that will be used.
  71.  
  72. // Default to using template class
  73. #ifndef __NOT_USING_TEMPLATE_CLASS__
  74. #define __USING_TEMPLATE_CLASS__
  75. #endif
  76.  
  77. #include "vbdfile.h"
  78. #include "cacheptr.h"
  79. #include "mnode.h"
  80.  
  81. #ifdef __USING_TEMPLATE_CLASS__
  82. typedef CachePtr<Mnode> CachePointer; // Pointer to cache
  83. #endif
  84.  
  85. #ifdef __NOT_USING_TEMPLATE_CLASS__
  86. #include "cacheptr.h"
  87. typedef CachePtr CachePointer; // Pointer to cache
  88. #endif
  89.  
  90. // Function return status codes
  91. const int __DUPLKEY__       = -1;
  92. const int __ALLOCERR__      = -2;
  93. const int __NODE_OVERFLOW__ =  2;
  94.  
  95. // (B)alanced multi-way file-base (T)ree (H)eader stored with every tree
  96. struct BtreeHeader
  97.   FAU root_addr;
  98.   INT32 order;
  99.   INT32 num_entries;
  100.   INT32 num_nodes;
  101.   INT32 height;
  102. };
  103.  
  104. // (B)alanced multi-way file-base (T)ree class.
  105. class Btree
  106. {  
  107. public:
  108.   Btree(int CacheSize = 8) : f(0), cache(CacheSize), root(cache) { }
  109.   ~Btree() { Disconnect(); }
  110.  
  111. public:
  112.   int Connect(VBDFilePtr &fptr, int create, FAU bha=sizeof(FileHeader));
  113.   void Disconnect();
  114.   int Create(char *FName, FAU bha=sizeof(FileHeader));
  115.   int Open(char *FName, VBDFile::AccessMode mode, FAU bha=sizeof(FileHeader));
  116.   void Flush(int clear=0);
  117.   void Close() { Disconnect(); }
  118.   int Search(EntryKey &e);
  119.   int Search(char *key);
  120.   int FullSearch(const EntryKey &e);
  121.   int FullSearch(char *key, FAU object_address, FAU class_id);
  122.   int Add(char *key, FAU object_address = 0, FAU class_id = 0, int flush = 1);
  123.   int Add(EntryKey &e, int flush = 1);
  124.   int Remove(char *k, FAU object_address = 0, FAU class_id = 0, int flush = 1);
  125.   int Remove(EntryKey &e, int flush = 1);
  126.   int IsEmpty() const { return bh.num_entries == 0; }
  127.   int IsOpen() const { return f->IsOpen(); } 
  128.   int IsOK() const { return f->IsOK(); }
  129.   void ClearErr() { f->ClearErr(); }
  130.   FAU GetRootAddr() { return bh.root_addr; }
  131.   INT32 GetOrder() { return bh.order; }
  132.   INT32 GetNumEntries() { return bh.num_entries; }
  133.   INT32 GetNumNodes() { return bh.num_nodes; }
  134.   INT32 GetHeight() { return bh.height; }
  135.   VBDFilePtr GetFilePtr() { return f; }
  136.   FAU GetHeaderAddr() { return bh_addr; }
  137.   CachePointer GetRoot() { return root; }
  138.   void ReInit(int flush = 0);
  139.   int TestTree();
  140.   
  141. #ifdef __USING_TEMPLATE_CLASS__
  142.   Cache<Mnode> *GetCache() { return &cache; }
  143. #endif
  144.  
  145. #ifdef __NOT_USING_TEMPLATE_CLASS__
  146.   Cache *GetCache() { return &cache; }
  147. #endif
  148.  
  149.   int operator!() const { return f->operator!(); }
  150.   operator int() const { return f->operator const int(); }
  151.   
  152. protected:
  153.   void ReadHdr(); 
  154.   void WriteHdr();
  155.   int Insert(EntryKey &e, CachePointer t);
  156.   int Delete(EntryKey &e, CachePointer t);
  157.   void RestoreBalance(CachePointer p, int posn);
  158.   void RotateRight(CachePointer p, int posn);
  159.   void RotateLeft(CachePointer p, int posn);
  160.   void Merge(CachePointer p, int posn);
  161.  
  162. protected:
  163.   VBDFilePtr f;      // File the Btree is connected to
  164.   FAU bh_addr;       // Address of the Btree header
  165.   BtreeHeader bh;    // Btree header
  166.   CachePointer root; // Pointer to the root node
  167.   
  168. #ifdef __USING_TEMPLATE_CLASS__
  169.   Cache<Mnode> cache; // Node cache
  170. #endif
  171.  
  172. #ifdef __NOT_USING_TEMPLATE_CLASS__
  173.   Cache cache; // Node cache
  174. #endif
  175. };
  176.  
  177. #endif // __BTREE_HPP
  178. // ----------------------------------------------------------- // 
  179. // ------------------------------- //
  180. // --------- End of File --------- //
  181. // ------------------------------- //
  182.  
  183.